using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;

public class SkinnProCustomUtility
{
    private static Transform tempRoot;
    public static Transform TempRoot
    {
        get
        {
            if (tempRoot == null)
            {
                tempRoot = GameObject.Find("TempRoot")?.transform;
                if (tempRoot == null)
                {
                    tempRoot = new GameObject("TempRoot").transform;
                    tempRoot.transform.position = Vector3.zero;
                }
            }

            return tempRoot;
        }

        set
        {
            if (value == null)
            {
                GameObject.DestroyImmediate(tempRoot.gameObject);
            }

            tempRoot = value;
        }
    }

    public static void DrawColorLabel(string label, Color color)
    {
        GUI.color = color;
        GUILayout.Label(label);
        GUI.color = Color.white;
    }

    public static void DisplayProgressBar(bool display = true, string title = "", string info = "", float progress = 0)
    {
        if (!display)
        {
            EditorUtility.ClearProgressBar();

            return;
        }

        if (string.IsNullOrEmpty(info))
        {
            info = "Executing...";
        }

        EditorUtility.DisplayProgressBar(title, info, progress);
    }

    public static GUILayoutOption[] ObjectFieldOption()
    {
        return new GUILayoutOption[]
        {
            GUILayout.Width(500)
        };
    }

    public static GUILayoutOption[] ButtonOption()
    {
        return new GUILayoutOption[]
        {
            GUILayout.Width(300),
        };
    }

    /// <summary>
    /// 物体是不是一个场景内的物体（脱离了Prefab）
    /// </summary>
    /// <param name="target"></param>
    /// <returns></returns>
    public static bool IsNotPrefab(GameObject target)
    {
        var type = PrefabUtility.GetPrefabAssetType(target);
        var status = PrefabUtility.GetPrefabInstanceStatus(target);
        if (type != PrefabAssetType.NotAPrefab || status != PrefabInstanceStatus.NotAPrefab)
        {
            UnityEngine.Debug.LogError("请从Scene中指定，Unpack的人物模型");

            return false;
        }

        return true;
    }

    public class TimeRecord : IDisposable
    {
        public Stopwatch sw;
        public string desc;

        public TimeRecord(string funcDesc)
        {
            desc = funcDesc;
            sw = new Stopwatch();
            sw.Start();
        }

        public void Dispose()
        {
            sw.Stop();
            UnityEngine.Debug.LogFormat("{0} 执行用时: {1} ms", desc, sw.ElapsedMilliseconds);
        }
    }
}
